﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AFCEPF.BankApplication.DAL
{
    /// <summary>
    /// Class that contains all needed informations
    /// about a property that is decorated with 
    /// a FieldAttribute.
    /// </summary>
    public class MappedProperty
    {
        private string      _propertyName;
        private string      _propertyFieldName;
        private bool        _isPrimaryKey;
        private DataMapper  _mappedObject;

        public MappedProperty(PropertyInfo property, DataMapper mapper)
        {
            _mappedObject = mapper;
            _propertyName = property.Name;

            // on récupère l'instance de notre attribut
            // de type FieldAttribute..
            FieldAttribute attribute = ReflectionHelper.GetUniqueAttribute<FieldAttribute>(property);            
            // ou..
            //FieldAttribute attribute = property.GetUniqueAttribute<FieldAttribute>();

            // et l'on va interroger ses propriétés..
            _propertyFieldName  = attribute.FieldName;
            _isPrimaryKey       = attribute.IsPrimaryKey;

        }

        /// <summary>
        /// Gets or sets the property value
        /// dynamically via reflection..
        /// </summary>        
        public object PropertyValue
        {
            get 
            {
                return ReflectionHelper.GetPropertyValue(_mappedObject, _propertyName);
            }
            set
            {
                ReflectionHelper.SetPropertyValue(_propertyName, _mappedObject, value);
            }
        }



        /// <summary>
        /// Gets the mapped object.
        /// </summary>
        public DataMapper MappedObject
        {
            get { return _mappedObject; }            
        }



        /// <summary>
        /// Gets a value indicating whether this instance is primary key.
        /// </summary>
        public bool IsPrimaryKey
        {
            get { return _isPrimaryKey; }            
        }



        /// <summary>
        /// Gets the name of the property field.
        /// </summary> 
        public string PropertyFieldName
        {
            get { return _propertyFieldName; }            
        }

        
        /// <summary>
        /// Gets or sets the name of the property.
        /// </summary>
        public string PropertyName
        {
            get { return _propertyName; }            
        }
    }
}
